package com.winchain.chainbill.asset.biz.credit.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.stellar.StellarConfiguration;
import com.winchain.chainbill.asset.biz.credit.AssetEvent;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CreditAssetTranSubscriber extends TxSubscriber<AssetEvent<AcctTxRec>> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public void onApplicationEvent(AssetEvent<AcctTxRec> t) {
		String txSn = SystemUtil.getUUID();
		List<AcctTxRec> records = t.getRecords();

		List<AcctTxRec> list = new ArrayList();
		String grpId = t.getStringValue("grpId");
		AcctTxRec tranCredit;
		for (AcctTxRec record : records) {
			tranCredit = getAcctTxRec(record, grpId);

			tranCredit.setTxSn(txSn);
			list.add(tranCredit);
		}
		Map<String, String> result;
		if (list.size() > 0) {
			String source = ((AcctTxRec) list.get(0)).getTxPassword();
			result = this.tradeService.fire(source, list);
			for (AcctTxRec txRec : list) {
				txRec.setTxSnHost((String) result.get("tx_sn"));
				t.setValue("txSnHost", txRec.getTxSnHost());
				if ("S".equals(result.get("ret_code"))) {
					this.tradeService.callback(txRec);
				} else {
					this.tradeService.rollback(txRec);
				}
			}
		}
	}

	private AcctTxRec getAcctTxRec(AcctTxRec txRec, String grpId) {
		txRec.setTxId(SystemUtil.getUUID());

		txRec.setTxChnCode("C");

		String finId = CommonDataUtil.getCurEntId();
		String cusId = txRec.getBizId();
		BigDecimal billAmt = txRec.getTxAmt();
		String abs = "��������";
		String txAcctType = "1";
		String cpAcctType = "1";

		AcctBas cpAcct = null;
		AcctBas txAcct = null;
		AcctBkp txAcctBkp = null;
		AcctBkp cpAcctBkp = null;
		txAcct = getAcctBas(cusId, txAcctType, null);

		String conBizType = "40";
		if (AssetCode.XTB.name().equals(txRec.getCurrCode())) {
			conBizType = "41";
		} else if (AssetCode.FQT.name().equals(txRec.getCurrCode())) {
			conBizType = "42";
		}
		if ("qt_red".equals(txRec.getOptType())) {
			txAcctBkp = this.billAcctBlcService.getCreditAcctBkpByCont(finId, cusId, txRec.getCurrCode(), conBizType);

			cpAcct = getAcctBas(finId, cpAcctType, null);
			cpAcctBkp = this.billAcctBlcService.getAcctBkp(cpAcct.getAcctNo(), txRec.getCurrCode(), null, null);
		} else if ("qt_inc".equals(txRec.getOptType())) {
			txAcct = getAcctBas(finId, txAcctType, null);
			txAcctBkp = this.billAcctBlcService.getAcctBkp(txAcct.getAcctNo(), txRec.getCurrCode(), null, null);
			cpAcct = getAcctBas(cusId, cpAcctType, null);
			cpAcctBkp = this.billAcctBlcService.getCreditAcctBkpByCont(finId, cusId, txRec.getCurrCode(), conBizType);
		}
		if ("qt_red".equals(txRec.getOptType())) {
			txRec.setTxAcctNo(txAcct.getAcctNo());
			txRec.setTxAcctNm(txAcct.getAcctNm());
			txRec.setTxBankNm(txAcct.getFiNm());
			txRec.setTxBkpId(txAcctBkp.getBkpId());
			txRec.setTxPassword(txAcct.getAcctKey());
			txRec.setTxAmt(billAmt);

			txRec.setCpAcctNo(StellarConfiguration.getCentralbankAddr());
			txRec.setCpBkpId("COLD_WALLET");
		} else if ("qt_inc".equals(txRec.getOptType())) {
			txRec.setTxAcctNo(StellarConfiguration.getCentralbankAddr());
			txRec.setTxPassword(StellarConfiguration.getCentralbankSeed());
			txRec.setTxBkpId("COLD_WALLET");
			txRec.setTxAmt(billAmt);

			txRec.setCpAcctNo(cpAcct.getAcctNo());
			txRec.setCpAcctNm(cpAcct.getAcctNm());
			txRec.setCpBankNm(cpAcct.getFiNm());
			txRec.setCpBkpId(cpAcctBkp.getBkpId());
		}
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxSts("I");

		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);

		txRec.setDataScLvl("B");

		txRec.setTxPassword(txAcct.getAcctKey());
		return txRec;
	}
}